<template>
  <div>
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- 循环渲染选择部门负责人数据 -->
          <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeesSimple } from '@/api/employees'
// 新增
import { addDepartments, getDepartmentsId, putDepartments } from '@/api/departments'

export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // value 内容  callback  输出的
    const validCode = (rule, value, callback) => {
      // console.log(value)
      // console.log(this.originList)
      let codeList = this.originList.map(item => item.code)
      // 编辑的状态 先移除自己的编码
      if (this.edit) {
        codeList = this.originList.filter(item => item.id !== this.id).map(item => item.code)
      }
      if (codeList.includes(value)) {
        callback(new Error(`部门编码 ${value} 已存在`))
      } else {
        callback()
      }
    }
    const validName = (rule, value, callback) => {
      console.log(value)
      // console.log(this.originList)
      // 找到pid和点击的部门id相同的子部门
      let nameList = this.originList.filter(item => item.pid === this.id).map(item => item.name)
      console.log(nameList)
      if (this.edit) {
        // 找到子部门pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // console.log(pid)
        const hr = this.originList.filter(item => item.pid === pid)
        // 排除自己
        nameList = hr.filter(item => item.id !== this.id).map(item => item.name)
      }
      if (nameList.includes(value)) {
        callback(new Error(`部门名称 ${value} 已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门管理者', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: 'blur' }
        ]
      },
      // 定义数组 储存数据
      list: []
    }
  },
  created() {
    // 打开表单 渲染数据
    this.loadEmployee()
    // 编辑  数据回显
    if (this.isEdit) {
      this.loadDepartmentsId()
    }
  },
  methods: {
    // 确定
    hSubmit() {
      // 表单校验
      this.$refs.deptForm.validate((valid) => {
        if (!valid) {
          this.isEdit ? this.hEdit() : this.hAdd()
        }
      })
    },
    // 新增
    async hAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 添加
    async hEdit() {
      try {
        const res = await putDepartments(this.form)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 数据回显
    async loadDepartmentsId() {
      try {
        const res = await getDepartmentsId(this.id)
        console.log(res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    hCancel() {},
    async loadEmployee() {
      try {
        // 获取简单员工列表
        const res = await getEmployeesSimple()
        // console.log(res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
